package com.hunlian.makelove.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hunlian.makelove.R;
import com.hunlian.makelove.common.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class SplashActivity extends BaseActivity implements OnPageChangeListener{
	public static boolean isForeground = false;
	
	private TextView tv_splash_content;
	private List<View> views;
	private ViewPager vp_first_open;
	private int currentIndex;
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				spf.edit().putBoolean("firstOpen", false).commit();
				startActivity(new Intent(SplashActivity.this, MainActivity.class));
				finish();
				break;
			case 2:
				initGuideView();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	private ViewPagerAdapter vpAdapter;
	private SharedPreferences spf;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initSplashView();
	}
	/**
	 * 加载启动页面
	 */
	private void initSplashView(){
		setContentView(R.layout.act_splash);
		tv_splash_content = (TextView) findViewById(R.id.tv_splash_content);
		
		//test code
		new Thread(){
			@Override
			public void run() {
				super.run();
				
			    SystemClock.sleep(2000);
			    spf = getSharedPreferences("izichan_config", 0);
				boolean firstOpen = spf.getBoolean("firstOpen", true);
				//Printout.println("是否观看过引导页："+firstOpen);
				if(firstOpen){
					mHandler.sendEmptyMessage(2);
				}else{
					startActivity(new Intent(SplashActivity.this, MainActivity.class));
					finish();
				}
			}
			
		}.start();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	
	/**
	 * 加载开机引导图
	 */
	
	private void initGuideView(){
		setContentView(R.layout.view_first_open_pager);
		LayoutInflater inflater = LayoutInflater.from(this);
		views = new ArrayList<View>();
		views.add(inflater.inflate(R.layout.pager_one, null));
		views.add(inflater.inflate(R.layout.pager_two, null));
		views.add(inflater.inflate(R.layout.pager_three, null));
		views.add(inflater.inflate(R.layout.pager_four, null));
		views.add(inflater.inflate(R.layout.pager_one, null));
		vp_first_open = (ViewPager) findViewById(R.id.vp_first_open);
		//Printout.println("显示引导页");
		vpAdapter = new ViewPagerAdapter(views, mHandler);
		vp_first_open.setAdapter(vpAdapter);
		vp_first_open.setOnPageChangeListener(this);
	}
	
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}
	
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}
	
	@Override
	public void onPageSelected(int arg0) {
		if(arg0==4){
			mHandler.sendEmptyMessage(1);
		}
		
		setCurDot(arg0);
	}
	
	private void setCurDot(int positon){
		
		if(positon < 0 || positon > views.size() - 1 || currentIndex == positon){
			return;
		}
		
		currentIndex = positon;
	}
	
}
